#include "StdAfx.h"
#include "Dead6502v3.h"

void CDead6502v3::SetMachine(CMachine *m)
{
MachinePtr = m;
}

void CDead6502v3::SetOp(ReadFunc *r)
{
dead6502.op6502 = r;
}

void CDead6502v3::SetRead(ReadFunc *r)
{
dead6502.read6502 = r;
}

void CDead6502v3::SetWrite(WriteFunc *w)
{
dead6502.write6502 = w;
}

void CDead6502v3::SetUser(void *u)
{
dead6502.userdata = u;
}

void CDead6502v3::Init()
{
memset(&dead6502,0,sizeof(dead6502data));
dead6502_setdata(&dead6502);
dead6502_init();
}

void CDead6502v3::Reset()
{
dead6502_setdata(&dead6502);
dead6502_reset();
}

void CDead6502v3::IRQ()
{
dead6502_setdata(&dead6502);
dead6502_irq();
}

void CDead6502v3::NMI()
{
dead6502_setdata(&dead6502);
dead6502_nmi();
}

void CDead6502v3::SetOpPage(int p,u8 *d)
{
dead6502.oppages[p] = d;
}

void CDead6502v3::SetReadPage(int p,u8 *d)
{
dead6502.readpages[p] = d;
}

void CDead6502v3::SetWritePage(int p,u8 *d)
{
dead6502.writepages[p] = d;
}

void CDead6502v3::SetPage(int p,u8 *d)
{
SetOpPage(p,d);
SetReadPage(p,d);
SetWritePage(p,d);
}

u32 CDead6502v3::Execute(u32 c)
{
dead6502_setdata(&dead6502);
return(dead6502_exec(c));
}

u8 CDead6502v3::Read(u32 addr)
{
dead6502_setdata(&dead6502);
return(dead6502_read(addr));
}

void CDead6502v3::Write(u32 addr,u8 data)
{
dead6502_setdata(&dead6502);
dead6502_write(addr,data);
}

u32 CDead6502v3::GetNextOpcodePC()
{
dead6502_setdata(&dead6502);
return(dead6502_getnextopcodepc());
}
u32 CDead6502v3::GetCurrentOpcodePC()
{
dead6502_setdata(&dead6502);
return(dead6502_getcurrentopcodepc());
}
	
void CDead6502v3::SaveState(CState *s)
{
s->Write8(dead6502.a);
s->Write8(dead6502.x);
s->Write8(dead6502.y);
s->Write8(dead6502.s);
s->Write8(dead6502.f);
s->Write16(dead6502.pc);
}

void CDead6502v3::LoadState(CState *s)
{
dead6502.a = s->Read8();
dead6502.x = s->Read8();
dead6502.y = s->Read8();
dead6502.s = s->Read8();
dead6502.f = s->Read8();
dead6502.pc = s->Read16();
}

u32 CDead6502v3::Disassemble(char *dest,u32 p)
{
return(dead6502_disassembleopcode(dest,(u16)p));
}

u32 CDead6502v3::GetLastRead()
{
return(dead6502.lastread);
}

u32 CDead6502v3::GetLastWrite()
{
return(dead6502.lastwrite);
}

int CDead6502v3::InsideIRQ()
{
return(dead6502.needirq & CPU_IRQ ? 1 : 0);
}

int CDead6502v3::InsideNMI()
{
return(dead6502.needirq & CPU_NMI ? 1 : 0);
}

u32 CDead6502v3::GetPC()
{
return(dead6502.pc);
}

u8 CDead6502v3::GetOpcode()
{
return(Read(dead6502.pc));
}

char *CDead6502v3::GenerateRegData(char *dest)
{
sprintf(dest,
"$%04X\n"
"$%02X\n"
"$%02X\n"
"$%02X\n"
"$%02X\n"
"$%02X\n"
"\n"
"\n"
"$%04X\n"
"$%04X\n"
"$%04X\n",
	dead6502.pc,dead6502.a,dead6502.x,dead6502.y,dead6502.s,dead6502.f,
	Read(0xFFFA) | (Read(0xFFFB) << 8),
	Read(0xFFFE) | (Read(0xFFFF) << 8),
	Read(0xFFFC) | (Read(0xFFFD) << 8)
);
return(dest);
}
